package com.example.onlineeducation.base

import android.content.res.Configuration
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.paging.PagingData
import androidx.viewbinding.ViewBinding
import com.example.onlineeducation.R
import com.example.onlineeducation.databinding.ActionbarLayoutBinding
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.gyf.immersionbar.components.SimpleImmersionOwner
import com.gyf.immersionbar.components.SimpleImmersionProxy
import com.yc.jetpacklib.base.YcBaseFragmentPlus
import com.yc.jetpacklib.refresh.YcRefreshBaseUtil


/**
 * Creator: yc
 * Date: 2021/6/17 18:26
 * UseDes:
 */
abstract class BaseFragment<V : ViewBinding>(createVB: ((LayoutInflater, ViewGroup?, Boolean) -> V)? = null) :
    YcBaseFragmentPlus<V>(createVB), SimpleImmersionOwner {

    /**
     * 设置标题
     * @param titleName String                  标题名
     * @param leftClick Function1<View, Unit>?  左侧点击事件，空则默认finish
     * @param rightClick Function1<View, Unit>? 右侧点击事件
     * @param rightTv String?                   右侧文字
     * @param rightIv Int?                      右侧图片
     */
    protected fun ActionbarLayoutBinding.setTitle(
        titleName: String = "",
        isShowLeft: Boolean = false,
        leftClick: ((View) -> Unit)? = null,
        rightClick: ((View) -> Unit)? = null,
        rightTv: String? = null,
        rightIv: Int? = null,
        isShowLine: Boolean = true
    ) {
        //标题
        tvActionbarMid.text = titleName
        //显示Line下划线
        if (isShowLine) {
            vActionbarLine.visibility = View.VISIBLE
        } else {
            vActionbarLine.visibility = View.GONE
        }
        //左侧
        if (isShowLeft) {
            ivActionbarLeft.visibility = View.VISIBLE
        } else {
            ivActionbarLeft.visibility = View.GONE
        }
        ivActionbarLeft.setOnClickListener {
            if (leftClick == null) {
                activity?.finish()
            } else {
                leftClick(it)
            }
        }
        //右侧
        rightTv?.let { name ->
            tvActionbarRight.apply {
                visibility = View.VISIBLE
                text = name
                setOnClickListener { v ->
                    if (rightClick != null) {
                        rightClick(v)
                    }
                }
            }
        }
        rightIv?.let { resId ->
            ivActionbarRight.apply {
                visibility = View.VISIBLE
                setImageResource(resId)
                setOnClickListener { v ->
                    if (rightClick != null) {
                        rightClick(v)
                    }
                }
            }
        }
    }

    protected fun <Data : Any> YcRefreshBaseUtil<Data>.acSetPagingData(pagingData: PagingData<Data>) {
        this.setPagingData(requireActivity(), pagingData)
    }

    /**
     * ImmersionBar代理类
     */
    private val mSimpleImmersionProxy = SimpleImmersionProxy(this)

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        mSimpleImmersionProxy.isUserVisibleHint = isVisibleToUser
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        mSimpleImmersionProxy.onActivityCreated(savedInstanceState)
    }

    override fun onDestroy() {
        mSimpleImmersionProxy.onDestroy()
        super.onDestroy()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        mSimpleImmersionProxy.onHiddenChanged(hidden)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        mSimpleImmersionProxy.onConfigurationChanged(newConfig)
    }

    /* *
       * 是否可以实现沉浸式，当为true的时候才可以执行initImmersionBar方法
       * Immersion bar enabled boolean.
       *
       * @return the boolean*/

    override fun immersionBarEnabled(): Boolean {
        return true
    }

    override fun initImmersionBar() {

    }
}